package com.xxl.controller.impl;

import com.xxl.controller.BlogApi;
import com.xxl.dto.BlogDto;
import com.xxl.dto.HomeDto;
import com.xxl.dto.UserDto;
import com.xxl.dto.vo.BlogVo;
import com.xxl.service.BlogService;
import com.xxl.util.GlobalResult;
import com.xxl.util.JwtUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 个人博客
 * @author xxl
 * @date 2023/4/4
 */
@RestController
@RequestMapping(value = "blog",produces = "application/json")
public class BlogApiImpl implements BlogApi<Map<String ,Object>> {
    @Resource
    BlogService blogService;
    @Resource
    JwtUtil jwtUtil;
    @Resource
    GlobalResult resultUtil;


    @Override
    @PostMapping("/publish")
    public Map<String, Object> writeBlog(@RequestBody BlogVo blogVo)
    {
        int i = blogService.postBlog(blogVo.getTitle(), blogVo.getContent(), blogVo.getSimpleContent(), jwtUtil.getTokenUser().getUsername());

        if (i > 0) {
            return resultUtil.result(200,"发布成功");
        }

        return resultUtil.result(500,"发布失败");
    }

    @Override
    @DeleteMapping("delete")
    public Map<String, Object> deleteSelfBlog(@RequestParam("uuid") String uuid) {
        if (!validBlogUuid(uuid)) {
            return resultUtil.result(500,"["+uuid+"，不是你写的博客]");
        }
        int i = blogService.deleteBlogByUUID(uuid);
        if (i > 0) {
            return resultUtil.result(500,"删除成功");
        }
        return resultUtil.result(200,"删除失败");
    }

    @Override
    @PostMapping("modifyBlog")
    public Map<String, Object> modify(
            @RequestBody BlogVo blogVo,
            @RequestParam("uuid") String uuid)
    {
        if (!validBlogUuid(uuid)) {
            return resultUtil.result(500,"["+uuid+"，不是你写的博客]");
        }
        String title = blogVo.getTitle();
        String content = blogVo.getContent();
        String simpleContent = blogVo.getSimpleContent();
        if (title.isEmpty() && content.isEmpty() && simpleContent.isEmpty()) {
            return resultUtil.result(500,"参数不能全部为空");
        }
        BlogDto blogDto = new BlogDto();
        blogDto.setTile(!title.isEmpty() ? title:null);
        blogDto.setContent(!content.isEmpty() ? content:null);
        blogDto.setSimpleContent(!simpleContent.isEmpty()  ? simpleContent:null);
        int i = blogService.<BlogDto>modifyBlogByUUID(uuid, blogDto);
        if (i > 0) {
            return resultUtil.result(200,"修改成功");
        }

        return resultUtil.result(500,"修改失败");
    }

    @Override
    @GetMapping("collect/{uuid}")
    public Map<String, Object> collectBlogByUUID(@PathVariable("uuid") String uuid ) {
        if (validBlogUuid(uuid)) {
            return resultUtil.result(500,"自己的博客不可以收藏");
        }
        blogService.followingBlog(jwtUtil.getTokenUser().getUsername(), uuid);
        return resultUtil.result(200,"收藏博客成功");
    }

    @Override
    @GetMapping("canselCollect/{uuid}")
    public Map<String, Object> canselCollectBlogByUUID(@PathVariable("uuid") String uuid) {
        if (validBlogUuid(uuid)) {
            return resultUtil.result(500,"自己的博客不可以收藏");
        }
        blogService.cancelFollowingBlog(jwtUtil.getTokenUser().getUsername(), uuid);

        return resultUtil.result(200,"取消收藏博客成功");
    }

    @Override
    @PostMapping("/queryAllBlog")
    public Map<String, Object> queryAllSelfBlog(@RequestParam("username") String username) {
        if (jwtUtil.getTokenUser().getUsername().equals(username)) {
            return resultUtil.result(200,"查询成功",getSelfBlogs());
        }
        return resultUtil.result(500,"查询失败，用户名和参数名不一致");
    }

    @Override
    @GetMapping("allCollectBlog")
    public Map<String, Object> queryAllCollectBlogs() {
        List<String> list = blogService.queryAllBlogByUsername(jwtUtil.getTokenUser().getUsername());
        return resultUtil.result(200,"查询成功",list);
    }

    /**
     * 检查这个博客是否是自己写的
     * true 是 false不是
     */
    public boolean validBlogUuid(String uuid) {
        boolean isMyBlog = false;
        List<String> list =
                getSelfBlogs().stream()
                .map(BlogDto::getBlogUuid).filter(uuid::equals).toList();
        if (list.size() > 0 && list.get(0).equals(uuid)) {
            isMyBlog = true;
        }
        return isMyBlog;
    }

    /**
     * 返回自己写的所有博客
     */
    public List<BlogDto> getSelfBlogs() {
        List<BlogDto> blogDtoList = blogService.queryAllBLogByUsername(jwtUtil.getTokenUser().getUsername());
        return blogDtoList;
    }
}
